<?php
/**
 * User: jack
 */

namespace Wilead\ApiBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use FOS\RestBundle\Controller\Annotations\RequestParam;
use Wilead\IArtistBundle\Entity\Moments;
use Wilead\IArtistBundle\Entity\MonmentsAttachment;

class MomentsController extends ApiController
{

    /**
     * @ApiDoc(
     *      description="1301获取用户朋友圈列表"
     * )
     * @RequestParam(name="page", requirements="\d+", description="分页，可为空，默认第一页", nullable=true)
     *
     * @param Request $request
     * @param $id 用户ID
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function getComentsAction(Request $request, $id)
    {
        $user = $this->getDoctrine()->getRepository('WileadIArtistBundle:User')->find($id);

        $view = $this->getFOSView();
        if (!$user)
            $view->setStatusCode(404)->setData(array('info' => '用户不存在'));

        $friendIds = $this->getDoctrine()->getRepository('WileadIArtistBundle:User')->getFriendsIdsByUserId($user->getId());

        $moments = $this->getDoctrine()->getRepository('WileadIArtistBundle:Moments')->getMomentsQueryBuilder($friendIds, $id);

        $paginator = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
            $moments,
            $this->get('request')->query->get('page', 1),
            10
        );

        $result = array();

        foreach ($pagination as $v) {
            /** @var \Wilead\IArtistBundle\Entity\Moments $v */
            $record = array();
            $record['id'] = $v->getId();
            $record['content'] = $v->getContent();
            $record['publishAt'] = $v->getPublishAt()->getTimestamp();
            $record['userId'] = $v->getUser()->getId();
            $record['nickname'] = $v->getUser()->getNickname();
            $record['userNum'] = $v->getUser()->getSystemNum();
            $record['photo'] = $v->getUser()->getAvatar();
            $record['attachments'] = array();
            if (count($v->getAttachments()) > 0) {
                $avalancheService = $this->get('imagine.cache.path.resolver');
                foreach ($v->getAttachments() as $attachment) {
                    /** @var \Wilead\IArtistBundle\Entity\MonmentsAttachment $attachment */
                    $attach = array();
                    $attach['id'] = $attachment->getId();
                    $attach['thumb'] = $avalancheService->getBrowserPath($attachment->getUrl(), 'moments');
                    $attach['url'] = $attachment->getUrl();
                    $attach['uploadedAt'] = $attachment->getCreatedAt()->getTimestamp();
                    array_push($record['attachments'], $attach);
                }
            }
            array_push($result, $record);
        }

        $info = array();
        $info['data'] = $result;
        $info['total_page'] = $pagination->getPaginationData();

        $view->setStatusCode(200)->setData($info);

        return $view;
    }

    /**
     * @ApiDoc(
     *      description="1302发朋友圈"
     * )
     * @RequestParam(name="content", requirements="string", description="发布内容", nullable=false)
     * @RequestParam(name="attachments", requirements="string", description="附件地址，使用相对路径，多个地址用,分割，例如/uploads/1.jpg,/uploads/2.jpg", nullable=false)
     *
     * @param Request $request
     * @param $id 用户ID
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function postComentsContentAction(Request $request, $id)
    {
        $user = $this->getDoctrine()->getRepository('WileadIArtistBundle:User')->find($id);

        $view = $this->getFOSView();
        if (!$user)
            $view->setStatusCode(404)->setData(array('info' => '用户不存在'));

        $content = $request->request->get('content');
        $attachments = $request->request->get('attachments');

        $moments = new Moments();
        $moments->setUser($user);
        $moments->setContent($content);

        if (strlen($attachments) > 0) {
            $attachments = explode(',', $attachments);
            if (is_array($attachments)) {
                foreach ($attachments as $v) {
                    if (strlen($v) > 0) {
                        $attachment = new MonmentsAttachment();
                        $attachment->setUrl($v);
                        $attachment->setMonments($moments);
                        $moments->addAttachment($attachment);
                    }
                }
            }
        }

        $em = $this->getEntityManager();
        $em->persist($moments);
        $em->flush();

        $view->setStatusCode(200)->setData(array('msg' => '发布成功'));

        return $view;
    }

    /**
     * @ApiDoc(
     *      description="1303删除朋友圈记录"
     * )
     * @RequestParam(name="mid", requirements="\d+", description="朋友圈记录ID", nullable=false)
     *
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function removeCommentsContentAction(Request $request)
    {
        $mid = $request->request->get('mid');
        $moments = $this->getDoctrine()->getRepository('WileadIArtistBundle:Moments')->find($mid);

        $view = $this->getFOSView();
        if (!$moments)
            $view->setStatusCode(404)->setData(array('info' => '朋友圈记录不存在'));

        $em = $this->getEntityManager();
        $em->remove($moments);
        $em->flush();

        $view->setStatusCode(200)->setData(array('msg' => '删除朋友圈记录成功'));

        return $view;
    }
} 